package person.bangbang.im.Androidgin.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * a log framework for debugging.
 * 
 * usually speaking, there are 2 kind of logging, one is 
 * to LOACLLY log, one to NON-LOCALLY log.
 * use tools in this class, it's easier to debug.
 * 
 * @author bangbang.song@gmail.com
 */
public class Log {
	private static final String TAG = "Util.Log";
	

	public static final int LEVEL_E = 0;
	public static final int LEVEL_D = 1;
	public static final int LEVEL_W = 2;
	public static final int LEVEL_I = 3;
	public static final int LEVEL_V = 4;
	
	public static final String TAG_DELIMITER = ": ";
	public static final String V = "(V)";
	public static final String D = "(D)";
	public static final String W = "(W)";
	public static final String E = "(E)";
	public static final String I = "(I)";
	
	private  static CircleBuffer mBuffer = new CircleBuffer();
	private  static List<OnLogEvent> mOnLogListeners = new ArrayList<OnLogEvent>();

	public static void d(String tag, String msg) {
		android.util.Log.d(tag, msg);
		String log = tag + TAG_DELIMITER +  msg + "\n";
		mBuffer.write2buffer(D + log);
		doLog(LEVEL_D, log);
	}
	public static void e(String tag, String msg) {
		android.util.Log.d(tag, msg);
		String log = tag + TAG_DELIMITER +  msg + "\n";
		mBuffer.write2buffer(D + log);
		doLog(LEVEL_E, log);
	}
	public static void v(String tag, String msg) {
		android.util.Log.d(tag, msg);
		String log = tag + TAG_DELIMITER +  msg + "\n";
		mBuffer.write2buffer(D + log);
		doLog(LEVEL_V, log);
	}
	public static void w(String tag, String msg) {
		android.util.Log.d(tag, msg);
		String log = tag + TAG_DELIMITER +  msg + "\n";
		mBuffer.write2buffer(D + log);
		doLog(LEVEL_W, log);
	}
	public static void i(String tag, String msg) {
		android.util.Log.d(tag, msg);
		String log = tag + TAG_DELIMITER +  msg + "\n";
		mBuffer.write2buffer(D + log);
		doLog(LEVEL_I, log);
	}
	public static String dump(){
		return mBuffer.dump();
	}
	public static void regOnLogListener(OnLogEvent e) {
		mOnLogListeners.add(e);
	}
	public static void unRegOnLogListener(OnLogEvent e) {
		mOnLogListeners.remove(e);
	}
	public static void doLog(int level, String message) {
		for (OnLogEvent it : mOnLogListeners) 
			it.onLog(level, message);
	}
	public interface OnLogEvent {
		public void onLog(int level, String message);
	}
}
